﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace ConsultPoint.SharePoint.RibbonVisibility.ControlTemplates
{
    public partial class RibbonVisibilityControl : UserControl
    {

        bool _collapseFreeSpace = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            string values = GetValues();
            if (values == String.Empty || values == Helper.Constants.INHERITS)
                return;

           
            


            //never
            if (values == Helper.Constants.NEVER)
            {
                InjectScript();
                return;
            }

            //always
            if (values == Helper.Constants.ALWAYS)   
                return;
            

            bool hideForAnonymous = Boolean.Parse(values.Split(';')[0]); 
            //Anonymous
            if (SPContext.Current.Web.CurrentUser == null)
            {
                if (hideForAnonymous)
                    InjectScript();
                else
                    return;
            }
            else //connected user
            {
                string rule = values.Split(';')[1];
                
                foreach (string s in values.Split(';')[2].Split(','))
                {
                    try
                    {
                        SPGroup group = SPContext.Current.Web.SiteGroups[s];
                        if (group.ContainsCurrentUser)
                        {
                            if (rule == "DENY")
                            {
                                InjectScript();
                                return;
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                    catch { }
                }

                if (rule == "ALLOW")
                    InjectScript();

            }
        }

        private string GetValues()
        {
            SPWeb web = SPContext.Current.Web;

            while (web != null)
            {
                string value = GetValues(web);
                if (value != Helper.Constants.INHERITS)
                {
                    if (web.Properties.ContainsKey(Helper.Constants.KEY_COLLAPSE))
                        _collapseFreeSpace = Boolean.Parse(web.Properties[Helper.Constants.KEY_COLLAPSE]);
                    
                    return value;
                }
                else
                    web = web.ParentWeb;
            }

            return string.Empty;
        }

        private string GetValues(SPWeb web)
        {
            if (web.Properties.ContainsKey(Helper.Constants.KEY))
                return web.Properties[Helper.Constants.KEY];
            else
                return Helper.Constants.INHERITS;
        }

       
       

        private void InjectScript()
        {
            string scriptfile = "/_controltemplates/SPRibbonVisibility/RibbonVisibility.js";
            if (_collapseFreeSpace)
                scriptfile = "/_controltemplates/SPRibbonVisibility/RibbonVisibility_collapsed.js";


            ScriptManager.RegisterClientScriptInclude(this, typeof(RibbonVisibilityControl), "SPRibbonVisibility", scriptfile );
        }
    }
}
